package gest;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Date;

public class Receitas_Impl extends UnicastRemoteObject implements Receitas {

	private int idRec;
	private int codvi;
	public static Connection conn;
	private String dpres;
	private String demi;
	private String med;
	private String medi;
	private String far;
	private String ute;

	protected Receitas_Impl(int codvi) throws RemoteException {
		this.codvi = codvi;
		if (conn == null)
			conn = DBConnection.get_conn();
		java.sql.Statement st;
		try {
			st = conn.createStatement();
			java.sql.ResultSet rs = st
					.executeQuery("select idRec, re.dpres,re.demi,me.nome,farm.nome,ut.nome,medi.nome, re.codvi from Receitas re, Medicos me, Medicamentos medi, Farmacias farm, Utentes ut where re.idMed=me.idMed and re.idFarm=farm.idFarm and re.idUt=ut.idUt and re.idMedi=medi.idMedi and re.codvi="
							+ codvi);
			if (rs.first()) {
				this.idRec = rs.getInt(1);
				this.dpres = rs.getString(2);
				this.demi = rs.getString(3);
				this.med = rs.getString(4);
				this.medi = rs.getString(5);
				this.far = rs.getString(6);
				this.ute = rs.getString(7);
				this.codvi = rs.getInt(8);

			} else {
				st.execute("insert into Receitas (codvi) values (" + codvi
						+ ")", st.RETURN_GENERATED_KEYS);
				rs = st.getGeneratedKeys();
				rs.first();
				this.idRec = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public int get_idRec() throws RemoteException {
		return idRec;
	}

	public int get_codvi() throws RemoteException {
		return codvi;
	}

	public String get_dpres() throws RemoteException {
		return this.dpres;
	}

	public String get_demi() throws RemoteException {
		return this.demi;
	}

	public String get_medico() throws RemoteException {
		return this.med;
	}

	public String get_medicamento() throws RemoteException {
		return this.medi;
	}

	public String get_utente() throws RemoteException {
		return this.ute;
	}

	public String get_farmacia() throws RemoteException {
		return this.far;
	}

	public boolean set_dpres(String dpres) throws RemoteException {
		this.dpres = dpres;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set dpres ='" + dpres
					+ "' where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean set_demi(String demi) throws RemoteException {
		this.demi = demi;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set demi ='" + demi
					+ "' where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean set_medico(String med) throws RemoteException {
		this.med = med;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set idMed = (select idMed from Medicos where nome='"
					+ med + "') where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean set_medicamento(String medi) throws RemoteException {
		this.medi = medi;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set idMedi = (select idMedi from Medicamentos where nome='"
					+ medi + "') where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean set_utente(String ute) throws RemoteException {
		this.ute = ute;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set idUt = (select idUt from Utentes where nome='"
					+ ute + "') where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

	public boolean set_farmacia(String far) throws RemoteException {
		this.far = far;
		Statement st;
		try {
			if (conn == null)
				conn = DBConnection.get_conn();
			st = conn.createStatement();
			String query = "update Receitas set idFarm = (select idFarm from Farmacias where nome='"
					+ far + "') where idRec=" + this.idRec;
//			System.err.println(query);
			st.execute(query);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return true;
	}

}
